using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Scopa.Menu;
using Services;
using Scopa.GamePlay;
using Scopa.GameSound;
using Scopa.SaveData;
using Microsoft.Phone.Shell;
using Scopa.Utilities;

#if WINDOWS_PHONE
//using Advertisement;
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace Scopa.MainStateManagement
{
    /// <summary>
    /// Una lista che mantiene i punteggi di tutti i giocatori per ogni round di gioco
    /// </summary>
    public struct PartialScores
    {
        public static List<Score> scoreList;
    }

    /// <summary>
    /// Funzioni statiche (invocabili da una qualsiasi istanza di Game) che si
    /// occupano di attivare i componenti che caratterizzano ciascuna fase di gioco
    /// </summary>
    public static class StateManager
    {
        // Menus
        private static IntroMenu intro;
        //private static BackgroundMenu bgMenu;
        private static ScopaeBriscolaMenu scopaeBriscolaMenu;
        //private static StartMenu startMenu;
        //private static SettingsMenu settingsMenu;
        //public static GameSettingsMenu GameSettings;
        //private static NewGameMenu newGameMenu;
        //private static CreditsMenu creditsMenu;
        //private static HelpMenu helpMenu;
        //private static MenuPause menuPause;
        //public static KeyboardMenu keyboardMenu;
        //public static Games games;


        /// <summary>
        /// Avviamo il menu
        /// </summary>
        /// <param name="game"></param>
        static public void StartScopaeBriscolaMenu(this Game game)
        {
            game.Cleanup();

            // Menu and game components
            game.Components.Add(new InputManager(game));
            scopaeBriscolaMenu = new ScopaeBriscolaMenu(game, MenuDepths.FrontMenu);
            game.Components.Add(scopaeBriscolaMenu);
            game.Components.Add(new MenuSoundManager(game));
            var menu = game.Services.GetService(typeof(IScopaeBriscolaMenu)) as IScopaeBriscolaMenu;

            // Events
            scopaeBriscolaMenu.OnScopaClicked += () =>
                {
                    Scopa.ScopaStateManagement.ScopaStateManager.first_transition = true;
                    Scopa.ScopaStateManagement.ScopaStateManager.StartMenu(game);
                };

            scopaeBriscolaMenu.OnExitClicked += () =>
            {
                // Save the configuration
                //game.SaveConfig(context.currentConfig);

#if WINDOWS_PHONE
                game.Exit();
                //game.ShowAds();
#else
                game.Exit();
#endif
            };

        }

        /// <summary>
        /// Animazione Introduttiva
        /// </summary>
        /// <param name="game"></param>
        static public void Intro(this Game game)
        {
            game.Cleanup();

            game.Components.Add(new InputManager(game));
            game.Components.Add(new MenuSoundManager(game));
            intro = new IntroMenu(game, MenuDepths.FrontMenu);
            game.Components.Add(intro);

            intro.Animated = true;
            intro.OnStartGame += () => { game.StartScopaeBriscolaMenu(); };
            //intro.OnExit += () => { game.ShowAds(); };
        }


        /// <summary>
        /// Mostra la schermata dei credits
        /// </summary>
        /// <param name="game"></param>
        static public void ShowCredits(this Game game)
        {
            //first_transition = true;
            //game.Cleanup();
            //game.Components.Add(new InputManager(game));
            //scopaeBriscolaMenu = new MainMenu(game, MenuDepths.FrontMenu);
            //game.Components.Add(scopaeBriscolaMenu);
            //settingsMenu = new SettingsMenu(game, MenuDepths.MiddleMenu);
            //game.Components.Add(settingsMenu);
            //creditsMenu = new CreditsMenu(game, MenuDepths.BackMenu);
            //game.Components.Add(creditsMenu);
            //bgMenu = new BackgroundMenu(game, MenuDepths.Background);
            //game.Components.Add(bgMenu);

            ////To hide back button from SettingsMenu
            //settingsMenu.buttons.Remove(settingsMenu.buttons.ElementAt(0));

            //creditsMenu.Animated = true;
            //scopaeBriscolaMenu.Enabled = false;
            //settingsMenu.Enabled = false;
            //game.Components.Add(new MenuSoundManager(game));
            //var menu = game.Services.GetService(typeof(ICreditsMenu)) as ICreditsMenu;

            //menu.OnCBackClicked += () =>
            //{
            //    first_transition = true;
            //    game.StartMenu();
            //};

        }



        /// <summary>
        /// Mostra la tastiera
        /// </summary>
        /// <param name="game"></param>
        /// 
        static public void ShowKeyboard(this Game game, bool fromProfile)
        {
            //game.Cleanup();
            //game.Components.Add(new InputManager(game));
            //keyboardMenu = new KeyboardMenu(game, MenuDepths.ForeGround);
            //game.Components.Add(keyboardMenu);
            //startMenu = new StartMenu(game, MenuDepths.FrontMenu);
            //game.Components.Add(startMenu);
            //scopaeBriscolaMenu = new MainMenu(game, MenuDepths.FrontMenu);
            //game.Components.Add(scopaeBriscolaMenu);
            //settingsMenu = new SettingsMenu(game, MenuDepths.MiddleMenu);
            //game.Components.Add(settingsMenu);
            //creditsMenu = new CreditsMenu(game, MenuDepths.BackMenu);
            //game.Components.Add(creditsMenu);
            //bgMenu = new BackgroundMenu(game, MenuDepths.Background);
            //game.Components.Add(bgMenu);
            //keyboardMenu.fromProfile = fromProfile;

            //keyboardMenu.Animated = true;
            //scopaeBriscolaMenu.Enabled = false;
            //creditsMenu.Enabled = false;
            //settingsMenu.Enabled = false;
            //scopaeBriscolaMenu.Enabled = false;
            //startMenu.Enabled = false;

            //game.Components.Add(new MenuSoundManager(game));
            //var menu = game.Services.GetService(typeof(IKeyboardMenu)) as IKeyboardMenu;
            //menu.OnSaveClicked += () => { first_transition = true; game.StartMenu(); };
            //menu.OnExitClickedNoProfile += () => { first_transition = true; game.StartMenu(); };
        }

        static public void ShowAds(this Game game)
        {
            //game.Cleanup();
#if WINDOWS_PHONE
            //List<Advertisement.GameTitle> gamesList = new List<Advertisement.GameTitle>();
            //gamesList.Add(GameTitle.Dice3D);
            //gamesList.Add(GameTitle.EbookReader);
            //gamesList.Add(GameTitle.Enigma);
            //gamesList.Add(GameTitle.Flashlight);
            //gamesList.Add(GameTitle.Ideopuzzle);
            //gamesList.Add(GameTitle.RisikoDice);
            //gamesList.Add(GameTitle.SpaceBattle);
            //gamesList.Add(GameTitle.Crossroads);
            //var adsComponent = new Advertisement.AdsComponent(game, gamesList, 5.0f);
            //game.Components.Add(adsComponent);
#endif
        }

        static public bool first_transition = true;

        /// <summary>
        /// Per evitare interferenze tra una fase di gioco e l'altra, cancelliamo tutti
        /// i game components attivi al termine di ciascuna fase di gioco.
        /// </summary>
        /// <param name="game"></param>
        static private void Cleanup(this Game game)
        {
            TransitionScreen transition = null;
            if (first_transition == false)
                transition = new TransitionScreen(game);

            for (int i = 0; i < game.Components.Count; i++)
            {
                if (game.Components[i] is TrialComponent) continue;
                ((GameComponent)game.Components[i]).Dispose();
                i--;
            }
            //game.Components.Clear();
            if (first_transition == false)
                game.Components.Add(transition);
            else
                first_transition = false;
        }
    }
}
